package com.lock.serviceImpl;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lock.InitZookeeper;
import com.lock.service.LockService;

@Service
public class LockServiceImpl implements LockService{

	@Autowired
	private InitZookeeper initZookeeper;
	
	public boolean addLock(String path) {
		boolean res = true;
		CuratorFramework cf = initZookeeper.getCf();
		try {
			cf.create().forPath(path,"test".getBytes());
		} catch (Exception e) {
			res =  false;
			System.out.println(res);
			return res;
		}
		System.out.println(res);
	   return res;
		
	}

	public void releaseLock(String path) {
		try {
			CuratorFramework cf = initZookeeper.getCf();
			cf.delete().forPath(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
